package com.ymx.darling.generater;

import com.ymx.darling.generater.config.GlobalConfig;
import com.ymx.darling.generater.config.StrategyConfig;
import com.ymx.darling.generater.table.Table;

import java.util.List;

/**
 * Service类生成器
 * @author 爱Java的小于
 */
public class ServiceGenerator extends BaseGenerator {
    public ServiceGenerator(GlobalConfig globalConfig, StrategyConfig strategyConfig) {
        super(globalConfig, strategyConfig);
    }

    public void createService(List<Table> tableList) {
        tableList.forEach(table -> {
            // 将创建好的Service类写入到文件中
            String className = this.toUpperCaseFirstOne(this.convertToCamelCase(table.getName()));
            String serviceContent;
            if (strategyConfig.isSpringApplication()) {
                serviceContent = TemplateContent.getContent("spring-service");
            } else {
                serviceContent = TemplateContent.getContent("service");
            }
            String serviceFilePath = String.format("%s\\%s\\%sService.java", this.getProjectAbsolutePath(), strategyConfig.getServicePackageName(), className);
            this.createClass(serviceFilePath, serviceContent
                    .replaceAll("\\{basePackage}", globalConfig.getBasePackage())
                    .replace("{author}", globalConfig.getAuthor())
                    .replaceAll("\\{entity}", className));

            // 将创建好的Service实现类写入到文件中
            // 判断当前为spring应用程序
            String serviceMplContent;
            if (strategyConfig.isSpringApplication()) {
                serviceMplContent = TemplateContent.getContent("spring-service-mpl");
            } else {
                serviceMplContent = TemplateContent.getContent("service-mpl");
            }
            String serviceMplFilePath = String.format("%s\\%s\\mpl\\%sServiceMpl.java", this.getProjectAbsolutePath(), strategyConfig.getServicePackageName(), className);
            this.createClass(serviceMplFilePath, serviceMplContent
                    .replaceAll("\\{basePackage}", globalConfig.getBasePackage())
                    .replace("{author}", globalConfig.getAuthor())
                    .replaceAll("\\{entity}", className));
        });
    }
}
